<?php

namespace App\Http\Controllers;

use App\BO\Payment\PaymentFactory;
use App\BO\PaymentState\PaymentStateDefinition;
use App\Entities\ConvertibleCardEntity;
use App\Entities\ConvertibleCardSaleRecordEntity;
use App\Entities\PaymentModel;
use App\Http\Requests\PurchaseConvertibleCardRequest;
use App\ServicesNew\ConvertibleCardService;
use App\Utils\MoneyFormatUtil;
use App\VO\ConvertibleCard\Builder as ConvertibleCardVOBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 权益卡
 * Class ConvertibleCardController
 * @package App\Http\Controllers
 */
class ConvertibleCardController extends BaseController
{
    /**
     * 权益卡列表页
     * @param ConvertibleCardService $convertibleCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ConvertibleCardService $convertibleCardService)
    {
        $cardEntityList = $convertibleCardService->getEntityList();
        if (empty($cardEntityList)) {
            return $this->successJsonResult([]);
        }
        /** @var ConvertibleCardEntity $cl */
        foreach ($cardEntityList as $k => $cl) {
            if (!$cl->onSale()) {
                unset($cardEntityList[$k]);
            }
        }
        $vo = (new ConvertibleCardVOBuilder())->buildList($cardEntityList);
        return $this->successJsonResult($vo);
    }

    /**
     *  详情页
     * @param Request $request
     * @param ConvertibleCardService $convertibleCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request, ConvertibleCardService $convertibleCardService)
    {
        $cardEntity = $convertibleCardService->getEntityFromRequest($request);
        if (!$cardEntity) {
            return $this->showJsonResult(201, 'notfound');
        }
        $vo = (new ConvertibleCardVOBuilder())->buildDetail([
            'cardentity' => $cardEntity,
        ]);
        return $this->successJsonResult($vo);
    }

    /**
     * @api {post} /convertiblecard/purchase 购买权益卡
     * @apiGroup ConvertibleCard
     *
     * @apiParam {Number} convertibleCardId  地址ID
     */
    /**
     * 购买
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function purchase(PurchaseConvertibleCardRequest $request)
    {
        if (!$request->passValidate()) {  // 验证不通过
            return $this->showJsonResult(500, $request->getErrorMessage());
        }

        $convertibleCardEntity = $request->getConvertibleCardEntity();
        $userId = $this->getUserId();

        DB::beginTransaction();

        // 支付记录
        $paymentEntity = PaymentFactory::getNewPaymentInstance(
            PaymentModel::ORDER_TYPE_CONVERTIBLE_CARD,
            $convertibleCardEntity->getPrice()
        );
        $paymentEntity->save();

        // 关联
        $convertibleCardSaleRecordEntity = ConvertibleCardSaleRecordEntity::getNewInstance();
        $convertibleCardSaleRecordEntity->setUserId($userId);
        $convertibleCardSaleRecordEntity->setTradeNo($paymentEntity->getTradeNo());
        $convertibleCardSaleRecordEntity->setConvertibleCardId($convertibleCardEntity->getId());
        $convertibleCardSaleRecordEntity->setState(PaymentStateDefinition::STATE_WAITING_PAY);
        $convertibleCardSaleRecordEntity->save();

        DB::commit();

        return $this->successJsonResult(['tradeNo' => $paymentEntity->getTradeNo(), 'amount' =>
            MoneyFormatUtil::pennyToDollar($paymentEntity->getAmount()), ]);
    }
}
